package application;

import java.io.File;

import data.CityArea;

import utils.config.Config;
import fetch.paginegialle.Fetcher;

public class MainFetcher {
	
	
	public static void main(String[] args) throws Exception {
		MainFetcher mf = new MainFetcher();
		mf.fetch();
		System.out.println("Done");
	}
	
	
	private String OUT_DIR;
	private CityArea AREA;
	private String SEATYP;
	
	public MainFetcher() {
		
		Config config = new Config(this.getClass().getSimpleName()+".ser"); 
		
		OUT_DIR = (String)config.get("OUT_DIR");
		
		
		
		AREA = new CityArea((String)config.get("AREA_NAME"), 
				   (double[][])config.get("AREA_BBOX"),
				   (String[])config.get("BASE_IN_DIRS"));
		
		SEATYP = (String)config.get("SEATYP");
	}
	
	
	public void fetch() throws Exception {
		
		//  Create dir OUT_DIR+"/"+AREA.getName()+"/
		new File(OUT_DIR+"/"+AREA.getName()+"/").mkdir();
		
		System.out.println(AREA.getNrows()+" rows of data to be processed");
		Fetcher f = new Fetcher();
		for(int i=0; i<AREA.getNrows();i++) {
			for(int j=0; j<AREA.getNcols();j++) {
						
				double[] c = AREA.getCenter(i, j);
				double radius = AREA.getRadius();
				
				
				
				String xml_file = OUT_DIR+"/"+AREA.getName()+"/"+c[1]+"-"+c[0]+"-"+radius+".xml";
				String query = SEATYP+"?lat="+c[1]+"&lon="+c[0]+"&dist="+radius+"&format=xml";	
				
				f.fetch(query,xml_file);
				
				Thread.sleep(1000);
			}
			System.out.println((i+1)+" out of "+AREA.getNrows()+" rows completed!");
		}
	}
}
